<template>
  <view class="container">
    
   <!-- 搜索框 -->
   <view class="search-box">
		<i class="iconfont icon-sou">&#xe61f;</i>
       <input class="input" maxlength="50" v-model="keywords"  placeholder="请输入关键词进行搜索" @confirm="searchList" placeholder-class="placeholder"/>
      <view class="soubtn" @click="searchProducts">搜索</view>
   </view>
   
   <!-- 历史搜索 -->
    <view class="history">
      <view class="flex-between m-b-10 text-bold">
         搜索记录
         <image :src="$util.img('image/delete.png')" @click="deleteHistoryList" class="delimg"></image>
       </view>
      <view class="historyList">
        <view v-for="(item, index) in searchHistory" :key="index" @click="otherSearch(item)" class="historyItem">
          {{item}}
        </view>
      </view>
    </view>
    
    <goods page="mall" :goodsList="goodsList"></goods>
    
       
  </view>
</template>

<script>
  import goods from '../../components/goods/goods.vue';
  export default {
    components: {
      goods
    },
    data() {
      return {
        searchHistory: [],
        keywords: '',
        page: 1,
        goodsList: [],

      }
    },
    onLoad() {
      this.searchHistory = uni.getStorageSync('search_history') || [];
    },
    methods: {
      otherSearch(keywords) {
        this.keywords = keywords;
        this.page = 1;
        this.searchProducts();
      },
      deleteHistoryList() {
        uni.removeStorageSync('search_history');
        this.searchHistory = [];
      },
      searchProducts() {
        if(!this.keywords) return this.$util.showToast({title: '请输入搜索关键词'})

        this.$api.sendRequest({
          url: "/api/goodssku/page",
          data: {
            page: this.page,
						category_id: 0,
						keyword: this.keywords
          },
          success: res => {
            if(res.code == 0) {
             if(this.page == 1) this.goodsList = [];
             this.goodsList = [...this.goodsList, ...res.data.list]
            
              // 将搜索关键字添加到搜索历史中
              if (!this.searchHistory.includes(this.keywords)) {
                this.searchHistory.push(this.keywords);
                uni.setStorageSync('search_history', this.searchHistory);
              }

            } else {
              this.$util.showToast({title: res.message})
            }
          }
        })
      },
    }
  }
</script>

<style>
  page {
    background-color: #fff;
  }
</style>
<style lang="scss" scoped>
// 搜索
.search-box {
  position: relative;
  display: flex;
  align-items: center;
  margin: 0 24rpx;
  box-sizing: border-box;
  background: #FFFFFF;
  padding: 2rpx 10rpx 2rpx 20rpx;
  border-radius: 60rpx;
  border: 1px solid $color-title2;
  z-index: 99;
  .icon-sou {
  	  color: $color-title2;
  	  font-size: 40rpx;
  	  margin-right: 10rpx;
  }
  
  .placeholder {
    font-size: $font-size-sub;
    color: #BBBEC9;
  }
  
  .soubtn {
    width: 102rpx;
    height: 54rpx;
    line-height: 54rpx;
    text-align: center;
    background: $color-title2;
    color: #fff;
    border-radius: 40rpx;
    margin-left: auto;
  }
  
}


.history {
  margin: 30rpx;
  .delimg {
    width: 40rpx;
    height: 40rpx;
  }
  .historyList {
    display: flex;
    flex-wrap: wrap;
    .historyItem {
      margin: 20rpx 10rpx 0;
      padding: 0 24rpx;
      height: 56rpx;
      line-height: 56rpx;
      border-radius: 40rpx;
      font-size: $font-size-tag;
      color: #5F6680;
      background: #F5F5F7;
     }
  }
		
}
</style>
